//读取配置
var config = require('./config.json');
var logger = require('./log4js.js');

var client = require('./client.js');
var redis = require('./redis.js');
var md5=require("md5");
//dateFile
/*测试代码 */
/*var r_data = {
  oid:"1000201712201928588248bedc1"

};

for(var i=0;i<30;i++){
	redis.lpush(config.ORDER_KEY, JSON.stringify(r_data),function(err,res){
		if(err){
			console.log('插入失败');
			return;
		}
	});
}*/

/*-----------end-----------*/
//http://api.sup.nw.ylgw8.com:7999/checkorder

logger.info("start_order");

var arr = [
	{key:config.ORDER_KEY,wait:10},
	{key:config.ORDER_KEY+'_2',wait:1*1000},
	{key:config.ORDER_KEY+'_3',wait:5*1000},
	{key:config.ORDER_KEY+'_4',wait:10*1000},
	{key:config.ORDER_KEY+'_5',wait:30*1000},
	{key:config.ORDER_KEY+'_6',wait:1*60*1000},
	{key:config.ORDER_KEY+'_7',wait:5*60*1000},
	{key:config.ORDER_KEY+'_8',wait:10*60*1000},
	{key:config.ORDER_KEY+'_9',wait:30*60*1000},
	{key:config.ORDER_KEY+'_10',wait:60*60*1000},
	{key:config.ORDER_KEY+'_11',wait:1*60*60*1000},
	{key:config.ORDER_KEY+'_12',wait:2*60*60*1000},
	{key:config.ORDER_KEY+'_13',wait:5*60*60*1000},
];


for(var i=0;i<arr.length;i++){
	redis.lpopRedis(i,arr[i]['key'],arr[i]['wait'],function(index,r_data){
		
		sendData(r_data,index);
	});
}


//发送数据
function sendData(r_data,index)
{

	if(typeof r_data != "object" ){
		logger.warn('数据不是一个对象');
		return;
	}
	var url = config.RE_ORDER_URL;
	var method = "post";	
     //参数
     var params = {
		"oid":r_data.oid
     };
	//发送数据
	client.send(url,params,method,function(resData){

   		resData = JSON.parse(resData);
   		if(typeof resData != 'object'){
   			logger.warn('数据不是一个对象');
			return;
   		}
   		
     	if(resData && resData.code  && resData.code == 200){
     		logger.info('成功:'+ JSON.stringify(resData));
     	}else{
 			lpush(r_data,index);
     	}
	},function(err){
		logger.warn('系统错误：oid:'+ r_data.oid);
		lpush(r_data,index);
	});
}	

//放入队列
function lpush(r_data,index)
{
	
	r_data.time = Date.parse(new Date()); 
	if(index+1 < arr.length){
		redis.lpush(arr[index+1]['key'], JSON.stringify(r_data),function(err,res){
			if(err){
				logger.warn('redis错误,重新执行加入队列：'+JSON.stringify(r_data));
				redis.lpush(arr[index+1]['key'], JSON.stringify(r_data),function(err,res){
					if(err){
						logger.error('redis错误,不再执行插入：'+JSON.stringify(r_data));
						sendServer(r_data.oid,3);
					}
				});
				return;
			}
			logger.info(JSON.stringify(r_data)+",加入队列:"+arr[index+1]['key']);
		});
	}else{
		redis.lpush(config.ORDER_FAIL_KEY, JSON.stringify(r_data),function(err,res){
			if(err){
				logger.warn('redis错误');
				return;
			}
			logger.info(JSON.stringify(r_data)+",加入队列:"+config.ORDER_FAIL_KEY);
			logger.info(JSON.stringify(r_data)+"，回调平台:"+r_data.oid);
			sendServer(r_data.oid,3);
		});
	}
}

//1,成功 2，失败  3，异常
function sendServer(oid,status)
{

	var url = config.RE_SELLER_URL;
	var method = "put";	
     //参数
    var params = {
		status:status,
		order_no :oid,
		timestamp :  Date.parse(new Date())
	};
	params.sign = md5(params.order_no+params.status+params.timestamp+config.secret_key);

	//发送数据
	client.send(url,params,method,function(resData){
   		resData = JSON.parse(resData);
   		if(typeof resData != 'object'){
   			logger.warn('数据不是一个对象');
			return;
   		}
   		if(resData.code==200){
   			logger.info('status:'+status+',反馈成功:'+oid);
   		}

	},function(err){
		logger.warn('server error:'+oid);
	});


}


